<!-- 海外wms快速签入 -->
<template>
  <el-dialog :close-on-click-modal="false" :title="$tt('label_quick_checkin')" :visible.sync="dialogQuickReceiving"
    custom-class="dialog_big">
    <ex_super_table ref="MainTable" :model="tableComputedModel" :data="tableList" height="600">
 
      <template v-slot:temporaryAreaId="{ row }">

        <base_area_select :value="row.temporaryAreaId" :warehouseId="$loginUser.currentWarehouseId"
          @changeItem="(it) => changeArea(it, row)" />
      </template>

      <template v-slot:temporaryLocationId="{ row }">
        <base_location_select v-model="row.temporaryLocationId" :areaId="row.temporaryAreaId"
          @changeItem="(e) => e ? row.temporaryLocationCode = e.code : null" />
      </template>
    </ex_super_table>

    <span slot="footer" class="dialog_footer">
      <el-button @click="dialogQuickReceiving = false">{{ $tt('label_cancel') }}</el-button>
      <el-button type="primary" @click="submitQuickTackDelivery">{{ $tt('label_submit') }}</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  route_manual: true,
  components: {},
  data() {
    return {
      dialogQuickReceiving: false,
      tableList: [],
      tableBaseModel: [
        {
          key: 'temporaryAreaId', label: 'label_warehouse_area', slot: 'temporaryAreaId', width: 200,
          renderHeader: (h, { column }) => {
            return (<div>
              <div>{this.$tt(column.label)}</div>
              <ex_dropdown_auto label='label_unified_select' size='mini' storeData option={{ label: 'labelStr', value: 'id' }}
                
                url='/wms-api/base/WmsBaseAreaRpc/selectAllLabel.json' onCommand={this.selectAllArea} />
            </div>)
          },
        },
        { key: 'temporaryLocationId', label: 'label_warehouse_location', slot: 'temporaryLocationId', width: 200, },

      ],
    }
  },
  props: {
    tableModel: {
      type: Array,
      default: () => []
    },
    orderKeyId: String,
  },
  computed: {
    tableComputedModel: function () {
      return _.concat(this.tableModel, this.tableBaseModel)
    },
  },
  methods: {
    selectAllArea(e) {
      this.tableList.forEach(it => {
        if (it.temporaryAreaId != e.id) {
          this.changeArea(e, it)
        }

      })
    },
    changeArea(it, row) {
      if (!it) it = { code: null, id: null }
      this.$set(row, 'temporaryAreaId', it.id)
      this.$set(row, 'temporaryAreaCode', it.code)
    },
    show({ tableList }) {
      tableList.forEach(it => {
        it.temporaryAreaId = null
        it.temporaryLocationId = null

      })
      this.tableList = tableList

      this.dialogQuickReceiving = true
      // const recommendList = tableList
      // if (!_.isEmpty(recommendList)) {
      //   _.forOwn(_.groupBy(recommendList, v => this.type == 'PALLET' ? v.palletNo : v.boxNo), (v, k) => {
      //     recommendParam.push({
      //       boxMarker: k,
      //       putawayType: this.type,
      //       skuList: v.map(it => {
      //         return {
      //           skuId: it.skuId,
      //           skuMarker: k,
      //           putawayQty: (Number(it.receivingQty) - Number(it.putawayHasQty)),
      //         }
      //       })
      //     })
      //   })
      //   this.recommendLocation(recommendParam)
      // }

    },


    submitQuickTackDelivery() {

      if (this.tableList.some(it => !it.temporaryAreaId)) {

        return this.$message.error(this.$tt("tip_please_select_warehouse_area"))
      } else if (this.tableList.some(it => !it.temporaryLocationId)) {
        return this.$message.error(this.$tt("tip_please_select_warehouse_location"))
      }

      let receivingList = this.tableList.map(it => {
        return {
          id: it[this.orderKeyId],
          temporaryAreaId: it.temporaryAreaId,
          temporaryAreaCode: it.temporaryAreaCode,
          temporaryLocationId: it.temporaryLocationId,
          temporaryLocationCode: it.temporaryLocationCode
        }
      })
      this.$ajax({
        url: "/wms-api/inbound/WmsInboundOverseasInboundOrderRpc/receivingBatch.json",
        method: 'post',
        type: 'json',
        loading: this.$tt("tip_submiting"),
        data: { receivingList },
        success: (res) => {
          this.$message.success(this.$tt("tip_submit_success"))

          this.dialogQuickReceiving = false
          this.$emit("submitSuccess")
        },
      })

    },
    recommendLocation(boxList) {
      this.$ajax({
        url: "/wms-api/base/WmsBasePutawayStrategyRpc/recommendLocation.json",
        method: 'post', type: 'json',
        loading: "正在查找推荐库位",
        data: { boxList: boxList },
        success: (res) => {
          if (res && res.data && res.data.boxList) {
            res.data.boxList.forEach(box => {
              const palletType = box.putawayType == 'PALLET'
              const box_detail = palletType ? this.tableData.filter(it => it.palletNo == box.boxMarker) : this.tableData.filter(it => it.boxNo == box.boxMarker)
              box.skuList.forEach(sku => {
                const item = _.find(box_detail, it => it.skuId == sku.skuId)
                if (item) {
                  this.$set(item, 'recommended', sku.recommendedLocationList)
                }
              })
            })
          }
        }
      })

    },

  }
}
</script>
